package ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import databaseConnect.SimulationExecutionRequestProcessor;
import datatypes.DiscoveryMechanismEnum;

public class SimulationExecutionComboBoxPanel extends JPanel implements ActionListener {

	private static int indexCounter = 0;
	public Vector<DiscoveryMechanismEnum> mechs = new Vector<DiscoveryMechanismEnum>();
	private JButton locButton = null;
	private JButton getDoneButton (){
		if (locButton == null){
			locButton = new JButton("Done!");
			locButton.setActionCommand("done");
		}
		return locButton;
	}
	
	public void addListener (ActionListener listener){
		getDoneButton().addActionListener(listener);
	}
	
	public SimulationExecutionComboBoxPanel (int SimulationID, DiscoveryMechanismEnum preselection){
		Vector<String> tmp = SimulationExecutionRequestProcessor.getScenarioNamesForSimulation(SimulationID);
		
		this.setLayout(new GridLayout(tmp.size()+1,2));
		for (int i = 0; i < tmp.size() ; i++){
			this.add(new JLabel(tmp.get(i) + ": "));
			this.add(this.getCombo(preselection));
			mechs.add(preselection);
		}
		this.add(getDoneButton());
		this.add(getDoneButton());
	}

	
	private JComboBox getCombo(DiscoveryMechanismEnum preselection) {
		JComboBox combo = null;
		if (combo == null) 
		{
			combo = new JComboBox(DiscoveryMechanismEnum.values());
			combo.setSelectedItem(preselection);
			combo.addActionListener(this);
			combo.setVisible(true);
			combo.setActionCommand(indexCounter++ + "");
		}
		return combo;
	}
	
	public void actionPerformed(ActionEvent event)
	  {
		if (event.getSource() instanceof JComboBox && event.getActionCommand().length()<3){
			String cmd = event.getActionCommand();
			DiscoveryMechanismEnum changes = (DiscoveryMechanismEnum) ((JComboBox)event.getSource()).getSelectedItem();
			System.out.println("changes:" + changes);
			int cmd_int = Integer.parseInt(cmd);
			System.out.println("cmd_int:" + cmd_int);
			this.mechs.insertElementAt(changes, cmd_int);	
			this.mechs.remove(cmd_int+1);	
		}
		
	  }
}
